package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bigkoo.pickerview.adapter.NumericWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.hjq.widget.view.SwitchButton;
import com.lxj.xpopup.core.BottomPopupView;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;

public class BottomNormalTimeDialog extends BottomPopupView {

    private BottomNormalTimeDialog.DialogClickListener mDialogClickListener;
    private WheelView wv_second;
    private WheelView wv_third;
    private NumericWheelAdapter mSecondNumericWheelAdapter;
    private NumericWheelAdapter mThirdNumericWheelAdapter;
    private int hour;
    private int minute;

    public BottomNormalTimeDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_normal_time_view;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        wv_second = findViewById(R.id.options2);
        wv_third = findViewById(R.id.options3);
        View btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    mDialogClickListener.onConfirmClick(wv_second.getAdapter().getItem(wv_second.getCurrentItem()) + "",wv_third.getAdapter().getItem(wv_third.getCurrentItem()) + "");
                }
            }
        });
        setStyle();
        initWheelView();
    }

    private void setStyle() {
        wv_second.setGravity(Gravity.CENTER);
        wv_second.setCyclic(true);
        wv_second.setAlphaGradient(true);
        wv_second.setItemsVisibleCount(5);
        wv_second.setDividerColor(0x00000000);
        wv_second.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_second.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_third.setGravity(Gravity.CENTER);
        wv_third.setCyclic(true);
        wv_third.setAlphaGradient(true);
        wv_third.setItemsVisibleCount(5);
        wv_third.setDividerColor(0x00000000);
        wv_third.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_third.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));
    }

    private void initWheelView(){
        if( wv_second == null || wv_third == null){
            return;
        }
        mSecondNumericWheelAdapter  = new NumericWheelAdapter(0, 59);
        wv_second.setAdapter(mSecondNumericWheelAdapter);
        wv_second.setCurrentItem(this.hour);

        mThirdNumericWheelAdapter  = new NumericWheelAdapter(0, 59);
        wv_third.setAdapter(mThirdNumericWheelAdapter);
        wv_third.setCurrentItem(this.minute);

//        wv_first.setOnItemSelectedListener(new OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(int index) {
//                tvSelectedTime.setText(formatText());
//            }
//        });
        wv_second.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {

            }
        });
        wv_third.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {

            }
        });
    }

    public void setSelectedTime(int hour, int min){
        this.hour = hour;
        this.minute = min;
        initWheelView();
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable BottomNormalTimeDialog.DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick(String hour, String minute);
    }
}
